package com.shchuangzhi.smartguard.core.command;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.Grade;
import com.shchuangzhi.smartguard.core.NotifyMessage;
import com.shchuangzhi.smartguard.core.StudentState;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by j_hu on 5/14/17.
 */

public class SyncStudentStateHandler extends SyncMetaCommandRespHandler {
    public SyncStudentStateHandler(SyncMetaCommand cmd) {
        super("student.state.sync", cmd);
    }

    @Override
    public boolean handle(InputStream input) {
        boolean handleOk = false;
        try {
            Gson gson = new Gson();
            SyncMetaCommandRS<SyncStudentStateListRS> syncRS = (SyncMetaCommandRS<SyncStudentStateListRS>)gson.fromJson(
                    getString(input),
                    new TypeToken<SyncMetaCommandRS<SyncStudentStateListRS>>(){}.getType());
            if (syncRS != null && syncRS.getData().getList() != null) {
                getCommand().notify(NotifyMessage.info(
                        String.format("%s<%s>", Consts.INFO_SYNC_STUDENT_STATE, syncRS.getData().getList().size()), Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST));
                ArrayList<StudentState> list = new ArrayList<>();
                for(StudentStateRS s:syncRS.getData().getList()) {
                    StudentState state = new StudentState();
                    state.setCode(s.getCode());
                    state.setName(s.getName());
                    list.add(state);
                }
                if (getCommand().getDbHelper().groupAddStudentStates(list)) {
                    getCommand().getDbHelper().getSyncRecord().update(getName(), syncRS.getLastMetaUpdateTime());
                    getCommand().notify(NotifyMessage.info(
                            String.format("%s<%s>", Consts.INFO_SYNC_STUDENT_STATE_OK, syncRS.getData().getList().size()), Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST));
                    getCommand().notify(NotifyMessage.wrap(list, Consts.NOTIFY_STATE_BUTTON_REFRESH));
                    handleOk = true;
                } else {
                    getCommand().notify(NotifyMessage.info(
                            String.format("%s<%s>", Consts.INFO_SYNC_STUDENT_STATE_FAIL, syncRS.getData().getList().size()), Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST));
                }
            }
        }catch(Exception e) {
            getCommand().raiseError(e);
        }
        return handleOk;
    }

    public class SyncStudentStateListRS {
        private List<StudentStateRS> list;
        public List<StudentStateRS> getList() {
            return list;
        }
    }

    public class StudentStateRS {
        private int code;
        private String name;

        public int getCode() {
            return code;
        }
        public String getName() {
            return name;
        }
    }
}
